/**
 * 常用方法封装
 * @author wenqiang 
 **/


import {
  ModalType,
  ToastType,
  InterfaceUrlType,
  IsAndroidType,
  IsPhoneXType,
  ConstNumType,
  ShowLoadingType,
  JsonpType,
  SetUserInfoType,
  GetTokenType,
  IsLoginType,
  HrefType,
  UtilsType
} from './types/utils'


//接口地址
export const interfaceUrl: InterfaceUrlType = function () {
  return 'https://www.thorui.cn'
  //return 'https://test.thorui.cn'
  //return 'https://uat.thorui.cn'
  // return 'https://prod.thorui.cn'
};
export const toast: ToastType = function (text, duration, success = false) {
  uni.showToast({
    title: text || "出错啦~",
    icon: success ? 'success' : 'none',
    duration: duration || 2000
  })
};
export const modal: ModalType = function (title, content, showCancel, callback, confirmColor, confirmText) {
  uni.showModal({
    title: title || '提示',
    content: content || '',
    showCancel: showCancel,
    cancelColor: "#555",
    confirmColor: confirmColor || "#5677fc",
    confirmText: confirmText || "确定",
    success(res) {
      if (res.confirm) {
        callback && callback(true)
      } else {
        callback && callback(false)
      }
    }
  })
};
// 判断是否是安卓设置
export const isAndroid: IsAndroidType = function () {
  const res = uni.getSystemInfoSync();
  return res.platform.toLocaleLowerCase() == "android"
}
// 判断是否是PhoneX
export const isPhoneX: IsPhoneXType = function () {
  const res = uni.getSystemInfoSync();
  let iphonex = false;
  let models = ['iphonex', 'iphonexr', 'iphonexsmax', 'iphone11', 'iphone11pro', 'iphone11promax']
  const model = res.model.replace(/\s/g, "").toLowerCase()
  if (models.includes(model)) {
    iphonex = true;
  }
  return iphonex;
};
export const constNum: ConstNumType = function () {
  let time = 0;
  // #ifdef APP-PLUS
  time = isAndroid() ? 300 : 0;
  // #endif
  return time
};
export const showLoading: ShowLoadingType = function (title, mask = true) {
  uni.showLoading({
    mask: mask,
    title: title || '请稍候...'
  })
};
export const jsonp: JsonpType = function (url, callback, callbackname) {
  // #ifdef H5
  let win = window as any;
  win[callbackname] = callback;
  let jsonScript = document.createElement("script");
  jsonScript.src = url;
  jsonScript.type = "text/javascript";
  document.head.appendChild(jsonScript);
  document.head.removeChild(jsonScript);
  // #endif
}
//设置用户信息
export const setUserInfo: SetUserInfoType = function (mobile, token) {
  //uni.setStorageSync("thorui_token", token)
  uni.setStorageSync("thorui_mobile", mobile)
}
//获取token
export const getToken: GetTokenType = function (name = "thorui_token") {
  return uni.getStorageSync(name)
}
//判断是否登录
export const isLogin: IsLoginType = function (name = "thorui_token") {
  return uni.getStorageSync(name) ? true : false
}
//跳转页面，校验登录状态
export const href: HrefType = function (url, isVerify = false) {
  if (isVerify && !isLogin()) {
    uni.navigateTo({
      url: '/pages/common/login/login'
    })
  } else {
    uni.navigateTo({
      url: url
    });
  }
}

const utils: UtilsType = {
  interfaceUrl,
  toast,
  modal,
  isAndroid,
  isPhoneX,
  constNum,
  showLoading,
  jsonp,
  setUserInfo,
  getToken,
  isLogin,
  href
}

export default utils
